
global name "B0_ui_prepare"
cap log close
log using "${pfile}/${name}.smcl", replace smcl

*Parameteres: YEAR   KINK  SLOPE BEN K-N MI MF   YF DF INF \ YEAR   KINK   SLOPE  BEN   K-N MI MF   YF  DF INF \ YEAR   KINK   SLOPE   BEN   K-N MI MF  YF  DF INF \
local y_2001 "2001, 225.00, 0.8, 180, 1, 4, 3, 2002, 31, 2.021 \ 2001, 297.14, -0.3, 235.31, 2, 4, 3, 2002, 31, 2.021 \ 2001, 495.28, -0.5, 335.88, 3, 4, 3, 2002, 31, 2.021"
local y_2002 "2002, 250.00, 0.8, 200, 1, 4, 3, 2003, 31, 1.877 \ 2002, 330.14, -0.3, 264.11, 2, 4, 3, 2003, 31, 1.877 \ 2002, 550.31, -0.5, 374.19, 3, 4, 3, 2003, 31, 1.877" 
local y_2003 "2003, 300.00, 0.8, 240, 1, 4, 3, 2004, 31, 1.668 \ 2003, 396.18, -0.3, 316.94, 2, 4, 3, 2004, 31, 1.668 \ 2003, 660.37, -0.5, 449.03, 3, 4, 3, 2004, 31, 1.668"
local y_2004 "2004, 325.00, 0.8, 260, 1, 4, 4, 2005, 31, 1.526 \ 2004, 429.20, -0.3, 343.36, 2, 4, 4, 2005, 31, 1.526 \ 2004, 715.40, -0.5, 486.46, 3, 4, 4, 2005, 31, 1.526"
local y_2005 "2005, 375.00, 0.8, 300, 1, 5, 3, 2006, 31, 1.419 \ 2005, 495.23, -0.3, 396.18, 2, 5, 3, 2006, 31, 1.419 \ 2005, 825.46, -0.5, 561.30, 3, 5, 3, 2006, 31, 1.419"
local y_2006 "2006, 437.50, 0.8, 350, 1, 4, 3, 2007, 31, 1.342 \ 2006, 577.77, -0.3, 462.22, 2, 4, 3, 2007, 31, 1.342 \ 2006, 963.04, -0.5, 654.85, 3, 4, 3, 2007, 31, 1.342"
local y_2007 "2007, 475.00, 0.8, 380, 1, 4, 2, 2008, 28, 1.302 \ 2007, 627.29, -0.3, 501.83, 2, 4, 2, 2008, 28, 1.302\ 2007, 1045.48, -0.5, 710.93, 3, 4, 2, 2008, 28, 1.302"
local y_2008 "2008, 518.75, 0.8, 415, 1, 3, 1, 2009, 31, 1.246 \ 2008, 685.06, -0.3, 548.05, 2, 3, 1, 2009, 31, 1.246 \ 2008, 1141.88, -0.5, 776.46, 3, 3, 1, 2009, 31, 1.246"
local y_2009 "2009, 581.25, 0.8, 465, 1, 2, 12, 2009, 31, 1.177 \2009, 767.60, -0.3, 614.08, 2, 2, 12, 2009, 31, 1.177 \ 2009, 1279.46, -0.5, 870.01, 3, 2, 12, 2009, 31, 1.177"
local y_2010 "2010, 637.50, 0.8, 510, 1, 1, 12, 2010, 31, 1.128 \ 2010, 841.88, -0.3, 673.50, 2, 1, 12, 2010, 31, 1.128 \ 2010, 1403.28, -0.5, 954.20, 3, 1, 12, 2010, 31, 1.128"
local y_2011 "2011, 675.00, 0.8, 540, 1, 1, 12, 2011, 31, 1.065 \ 2011, 891.40, -0.3, 713.12, 2, 1, 12, 2011, 31, 1.065 \ 2011, 1485.83, -0.5, 1010.35, 3, 1, 12, 2011, 31, 1.065"
local y_2012 "2012, 777.50, 0.8, 622, 1, 1, 12, 2012, 31, 1    \ 2012, 1026.77, -0.3, 821.41, 2, 1, 12, 2012, 31, 1    \ 2012, 1711.45, -0.5, 1163.75, 3, 1, 12, 2012, 31, 1   "
local y_2013 "2013, 847.50, 0.8, 678, 1, 1, 12, 2013, 31, 0.944  \ 2013, 1090.43, -0.3, 872.34, 2, 1, 12, 2013, 31, 0.944  \ 2013, 1817.56, -0.5, 1235.91, 3, 1, 12, 2013, 31, 0.944"
local y_2014 "2014, 905.00, 0.8, 724, 1, 1, 12, 2014, 31, 0.887  \ 2014, 1151.06, -0.3, 920.84, 2, 1, 12, 2014, 31, 0.887    \ 2014, 1918.62, -0.5, 1304.63, 3, 1, 12, 2014, 31, 0.887"
local y_2015 "2015, 985.00, 0.8, 788, 1, 1, 12, 2015, 31, 0.801  \ 2015, 1222.77, -0.3, 978.26, 2, 1, 12, 2015, 31, 0.801    \ 2015, 2038.15, -0.5, 1385.90, 3, 1, 12, 2015, 31, 0.801"
local y_2016 "2016, 1100, 0.8, 880, 1, 1, 12, 2016, 31, 0.753  \ 2016, 1360.70, -0.3, 1088.56, 2, 1, 12, 2016, 31, 0.753    \ 2016, 2268.05, -0.5, 1542.24, 3, 1, 12, 2016, 31, 0.753"

mat sch_y = ( `y_2001' \ `y_2002' \ `y_2003' \ `y_2004' \ `y_2005' \ `y_2006' \ `y_2007' \ `y_2008' \ `y_2009' \ `y_2010' \ `y_2011' \ `y_2012' \ `y_2013' \ `y_2014' \ `y_2015' \ `y_2016' )
matlist sch_y



** Recovering data on previous years - relevant for UI potential duration
forvalues year = 2002/2013 {
use "${prais}/`year'/2_`year's.dta",clear
keep dt_dism identificad causadesli remmedia cpf year
replace dt_dism = mdy(12,31,`year') if dt_dism == .
rename causadesli causadesli_prev
save "B0_temp`year'.dta", replace
}


*** Merging with information from previous years ***
forvalues year = 2005/2014 {
use "${prais}/`year'/2_`year's.dta", clear

drop if openjob==1

replace tempempr = tempempr*7/30 // setting tenure in months
drop if causadesli != 11  // Only workers laid-off against their will
drop if tempempr < 16 | tempempr > 30

gsort cpf dt_dism
by cpf dt_dism:g co = _n
keep if co==1
drop co
g term = dt_dism // term is date of spell of interest

// appending information on past 36 months
local l1year = `year' - 1
local l2year = `year' - 2
local l3year = `year' - 3
append using "B0_temp`l1year'.dta"
append using "B0_temp`l2year'.dta"
append using "B0_temp`l3year'.dta"

gegen max_year = max(year), by(cpf)
drop if max_year != `year' // dropping past spells of workers not dismissed in the current year
drop max_year

format term %d
gsort cpf year
by cpf: replace term = term[_N]

drop if (year(term)-year(dt_dism))*12 + (month(term)-month(dt_dism)) > 37 // droping spells ended more than 36 months prior to the dismissal

// keeping only workers with a single spell
gegen maxdt_hir = max(dt_hir), by(cpf)
gegen mindt_hir = min(dt_hir), by(cpf)

gegen maxid = max(identificad), by(cpf)
gegen minid = min(identificad), by(cpf)

keep if maxdt_hir==mindt_hir & maxid==minid
drop maxdt_hir mindt_hir maxid minid

// droping workers with multiple spells within a year during UI entitlement period
gsort cpf year
by cpf year: g spells = _N
gegen cpf_spells = max(spells), by(cpf)
drop if cpf_spells > 1
drop cpf_spells spells

replace causadesli_prev = -1 if year == `year'
bysort cpf: egen max_cause = max(causadesli_prev) // droping workers with other termination during UI entitlement period
drop if max_cause != 0
drop causadesli_prev max_cause

g l_remmedia = remmedia if year ==  `year' - 1
gegen prev_remmedia = mean(l_remmedia), by(cpf)
g x = prev_remmedia - 2
drop l_remmedia

drop if year != `year'
drop term

save "B0_`year'.dta", replace
}


// Appending relevant data for all years //

clear
forvalues year = 2005(1)2014 {
append using "B0_`year'.dta"
}

keep if tpv == 10
cap replace natjuridica = naturjur if natjuridica ==.
cap drop naturjur
drop if natjuridica <= 2038
keep if hor >= 30
drop if ind == 1

*** Creating Predicted Variables ***

xtile qt_rem = remmedr, n(10)

g age_dism = (dt_dism-dtb)/365
xtile qt_dism = age_dism, n(10)
replace qt_dism = 99 if age_dism == . // age at dismissal is not available for 2011 and 2012

gen abono = (x<=0)

replace school = school * 100
replace school = 0 if school == .
replace race = race + 10

g mesdesli = month(dt_dism)

set matsize 4000
reg und i.school i.ind i.qt_dism i.race gend i.hor i.year i.uf i.qt_rem i.mesdesli  i.abono

predict pred_und, xb

g und_5 = und
replace und_5 = 5 * 30/7 if und_5 > 5 * 30/7

reg und_5 i.school i.ind i.qt_dism i.race gend i.hor i.year i.uf i.qt_rem i.mesdesli  i.abono

predict pred_und_5, xb

g s1 = (und<=4)

reg s1 i.school i.ind i.qt_dism i.race gend i.hor i.year i.uf i.qt_rem i.mesdesli  i.abono

predict pred_s1, xb

g s2 = (und<=8)

reg s2 i.school i.ind i.qt_dism i.race gend i.hor i.year i.uf i.qt_rem i.mesdesli  i.abono

predict pred_s2, xb

g s3 = (und<=12)

reg s3 i.school i.ind i.qt_dism i.race gend i.hor i.year i.uf i.qt_rem i.mesdesli  i.abono

predict pred_s3, xb

replace race = race - 10

* compute ui benefit value
g ben = .
g ben_inf = .
foreach i in 11 12 13 14 {

local year = sch_y[1+(`i'-1)*3,1] 			// Year
local mon_i = sch_y[1+(`i'-1)*3,6]			// Initial Month of the UI Schedule
local mw = sch_y[1+(`i'-1)*3,4]			// Yearly Minimum Wage
di "year `year'"
replace ben = sch_y[1+(`i'-1)*3,4]  if remmedr <= sch_y[1+(`i'-1)*3,2] & year == `year' 	// Benefit at the Kink
replace ben = remmedr * 0.8  if remmedr > sch_y[1+(`i'-1)*3,2] & remmedr <= sch_y[2+(`i'-1)*3,2] & year == `year' 	
replace ben = sch_y[2+(`i'-1)*3,4] + (remmedr-sch_y[2+(`i'-1)*3,2]) * 0.5  if remmedr > sch_y[2+(`i'-1)*3,2] & remmedr <= sch_y[3+(`i'-1)*3,2] & year == `year' 
replace ben = sch_y[3+(`i'-1)*3,4] if remmedr > sch_y[3+(`i'-1)*3,2] & year == `year'
replace ben_inf = ben * sch_y[1+3*(`i'-1),10]	if year == `year' 	// Benefit at the Kink
}
drop ben

save "B0_ui.dta", replace


clear
forvalues year = 2001(1)2017 {
cap erase B0_`year'.dta
cap erase B0_temp`year'.dta
}


// call script preparing additional variables
do "${dbox}/Brazil_Cash_on_new/Do_restat/B0_ui_prepare_additionalvars.do"

// call script preparing weights
do "${dbox}/Brazil_Cash_on_new/Do_restat/B0_ui_prepare_weights.do"


// *** // FINAL SAMPLE - UI EXTENSIONS EFFECTS // *** //
use "B0_ui.dta", clear

merge 1:1 year id using B0_weights, keep(3) nogen // keep only sample priorly defined

merge 1:1 year id using B0_additional, keep(1 3) nogen
replace n_und = (mdy(12,31,2017) - dt_dism)/7 if n_und==.
replace und = n_und

replace mon12 = 0 if mon12==.
replace mon24 = 0 if mon24==.
replace mon36 = 0 if mon36==.
replace inc12 = 0 if inc12==.
replace inc24 = 0 if inc24==.
replace inc36 = 0 if inc36==.

g emp12 = mon12>0 & mon12!=.
g emp24 = mon24>0 & mon24!=.
g emp36 = mon36>0 & mon36!=.

g totmon = mon12+mon24+mon36
g totinc = inc12+inc24+inc36

g pretotmon = premon12+premon24+premon36
g pretotinc = preinc12+preinc24+preinc36
replace pretotmon = 0 if pretotmon == .
replace pretotinc = 0 if pretotinc == .

g totwage = totinc/totmon
g wage12 = inc12/mon12
g wage24 = inc24/mon24
g wage36 = inc36/mon36

g mig = mun!=n_mun if n_mun!=.
g miguf = int(mun/10^4) != int(n_mun/10^4) if n_mun!=.
g pub = n_natjur <=2038 if n_natjur !=.
g migocup = ocup != n_ocup if n_ocup!=.
g migocup2 = int(ocup/10^3) != int(n_ocup/10^3) if n_ocup!=.

g manager = int(n_ocup/10^5)==1 if n_ocup!=.
g whitec = int(n_ocup/10^5)>=2 & int(n_ocup/10^5)<=4 if n_ocup!=.
g blue = int(n_ocup/10^5)>=5 & int(n_ocup/10^5)<=9 if n_ocup!=.

g migind = ind != n_ind if n_ind!=.

foreach x in 3 5 6 36 { // everything monthly
g sur`x' = und > 30/7 * (`x')
g dur`x' = und
replace dur`x' = 30/7 * `x' if und >= 30/7 * `x'
}

g white = race==2
g cash =(x<=0)
g lnrem = ln(remmedr)
g ind_cons = ind==4
replace school= school/10

keep if tempempr >= 14 & tempempr <= 32

// keeping only necessary variables
keep year id weight w_ebal und ${var} cpf tempempr mesdesli remmedia school white ind_cons cash gend hor age_dism school ind qt_dism race gend hor year uf qt_rem mesdesli abono // remmedr remmedia school age pretotmon pretotinc school gend disab remmedr hor firm_size1 identificad radic year ind pred_s3 pred_und age_dism qt_dism qt_rem mun

save A72_boot_ui, replace // saving data for bootstrapping

// RD variables
g x = tempempr - 24
replace x = . if tempempr > 22  & tempempr <= 24 

g k = (x>=0&x!=.)
g kx = k*x

save B_ui, replace





cap log close



*do "C:/Users/admin-britto/Dropbox/Brazil_Cash_on_new/Do/B5_survey_prepare.do"
*do "C:/Users/admin-britto/Dropbox/Brazil_Cash_on_new/Do/B0_ui_prepare_additionalvars.do"